/**
 * Created by Administrator on 2017/7/7 0007.
 */
define(["jquery","bbcare","scprit/department/vm/queueManagement/createQueue-data.js",
    "scprit/department/vm/queueManagement/createQueue-ajax.js"],function($,$bb,$tpl,$ajax) {
    var eventTotal={};
    //目标人数输入数字
    eventTotal.isNumber=function(){
        $("#queue-number").on('blur',function(){
            var reg = /^[1-9]\d*$/;
            if(!reg.test($.trim($(this).val()))){
                $bb.toastShow("人数请输入整数",1500);
                $(this).val("").focus();
            }
            var curNum=$(this).attr("currNum");
            var val=$(this).val();
            if(curNum >= 0 && val ){
                if(val < curNum){
                    $bb.toastShow("当前人数已经超过目标人数，请重新修改");
                    $(this).val("");
                }
            }
        })
    };

    //全选：
    eventTotal.isAllChecked=function(){
        if($(this).is(":checked")){
            $("input[name='isCheck']").prop("checked",'true');
        }else{
            $("input[name='isCheck']").prop("checked",false);
        }
    };
    eventTotal.isChecked=function(){
        if(!$(this).is(":checked")) {
            $("#checkAll").prop("checked",false);
        }
    };
    //上传图片：
    eventTotal.toUpload=function(fn){
        $("#queue-logo").click();
        $("#myForm").on("change","input[type='file']",function(){
            var $file = $(this);
            var val=$file.val();
            var imgType=[".jpg",".jpeg",".gif",".bmp",".png"];
            var fileType=val.substring(val.lastIndexOf('.'));
            var fileObj = $file[0];
            var windowURL = window.URL || window.webkitURL;
            var dataURL;
            if($.inArray(fileType, imgType)!="-1"){
                if(fileObj && fileObj.files && fileObj.files[0]){
                    dataURL = windowURL.createObjectURL(fileObj.files[0]);
                    var img='<img src="'+dataURL+'" id="logo-img"/>';
                    $('#logo-click').html(img);
                    $ajax.upload(this,fileObj,fn);
                }
            }else{
                $bb.toastShow("仅支持上传jpg,png,jpeg,gif,bmp格式的图片");
            }
        })
    };
    //提交：
    eventTotal.toSubmit=function(id){
        var queueId,queueIcon,queueIntro,queueName,queueUsertype,schemeId,schemeVersionid,targetNum;
        queueId= id ? id : '';
        queueName= $.trim($("#queue-name").val());
        queueUsertype= $.trim($("#user-type").val());
        schemeId= $.trim($("#followup-hdfp").find("select").val());
        schemeVersionid=$("#followup-hdfp").find("select option:selected").attr("schemeVersion");
        queueIntro = $.trim($("#textarea").val());
        queueIcon = $(".queue-logo").attr("logo-src");
        targetNum=$.trim($("#queue-number").val());
        queueIcon=$("input[type='file']").attr('src');
        var queueAuthoritys=[];
        $("#doctor-roleList>tr").each(function(index,ele){
            if(index!="0"){
                if($(this).find("input").is(":checked")){
                    queueAuthoritys.push({
                        'doctorId':$(this).attr("doctorId"),
                        'role':$(this).find("select").val()
                    });
                }
            }
        });
        var params={
            queueAuthoritys:queueAuthoritys,
            queueInfo: {
                id:queueId,
                queueIcon:queueIcon,
                queueIntro:queueIntro,
                queueName:queueName,
                queueUsertype:queueUsertype,
                schemeId:schemeId,
                schemeVersionid:schemeVersionid,
                targetNum:targetNum
            }
        };
        // console.log(params);
        var submitFn=function(data){
            $bb.openPage('queueManagement');
        };
        if(queueName && queueUsertype && schemeId ){
            $ajax.toSubmit(params,submitFn);
        }else{
            $bb.toastShow("* 标记处为必填项目");
        }
    };
    return eventTotal;
});
